package com.zjpavt.client.page;

import com.zjpavt.client.Connection;
import com.zjpavt.client.command.offlinecommand.OfflineCommandManager;
import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.hal.LoopManager;
import com.zjpavt.client.page.pageunit.ScreenLine;
import com.zjpavt.client.job.JobManager;
import com.zjpavt.client.page.abstractpage.AbstractEditablePage;
import com.zjpavt.client.page.editableunit.EditUnitString;

import java.util.ArrayList;
import java.util.List;

/**
 * 第十页 显示当前通道状态页面
 * @author zyc
 */
public class ChannelStatusPage extends AbstractEditablePage implements LoopManager.OnLoopChangeListener {

    private static String[] valueList = null;

    public ChannelStatusPage(int pageNum, long mRefreshPeriod) {
        super(pageNum, mRefreshPeriod);
    }

    @Override
    public void initEditUnits() {
        valueList = new String[] {"D"};
        addEditUnit(new EditUnitString(4, 15, 15, valueList));
    }

    @Override
    public void onVisibleChanged(boolean isVisible) {
        super.onVisibleChanged(isVisible);
        if (isVisible) {
            LoopManager.getInstance().addLoopListener(this);
        } else {
            LoopManager.getInstance().removeLoopListener(this);
        }
    }

    @Override
    public void refreshPage() {
        int[] loopStatus = LoopManager.getInstance().getEighthLoopStatus();
        StringBuilder loopStatusSb = new StringBuilder();
        for (int state : loopStatus) {
            loopStatusSb.append(state);
        }
        LedScreen.getInstance().setText(getPageContent(loopStatusSb.toString()));
    }

    private List<ScreenLine> getPageContent(String channelState) {
        List<ScreenLine> result = new ArrayList<>();
        result.add(new ScreenLine.ScreenLineBuilder()
                   .text("CH_state")
                   .pageNum(getPageNum())
                   .build());
        if(channelState.length() < LedScreen.SCREEN_WIDTH) {
            result.add(new ScreenLine.ScreenLineBuilder()
                       .text(channelState.substring(0, channelState.length()))
                       .build());
            result.add(new ScreenLine.ScreenLineBuilder().build());
        } else {
            result.add(new ScreenLine.ScreenLineBuilder()
                       .text(channelState.substring(0, LedScreen.SCREEN_WIDTH))
                       .build());
            result.add(new ScreenLine.ScreenLineBuilder()
                       .text(channelState.substring(LedScreen.SCREEN_WIDTH,Math.min(LedScreen.SCREEN_WIDTH*2,channelState.length())))
                       .build());
        }
        result.add(new ScreenLine.ScreenLineBuilder().text("D")
                   .alignRight()
                   .build());
        return result;
    }

    @Override
    public void onLoopChanged() {
        refreshPage();
    }


    /**
     * 请求离线数据使用异步 避免造成页面阻塞
     */
    @Override
    public void saveVal() {
        if (OfflineCommandManager.deleteAllOfflineCommand()) {
            JobManager.getInstance().cancelOfflineCommandJob();
            new Thread(()-> OfflineCommandManager.requestForOfflineCommand(Connection.getInstance())).start();
        }
    }

    @Override
    public void setDefaultVal() {
        getEditUnit(0).setCurrentVal(0);
    }
}
